//
//  YHYAddWorkViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/4/19.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYAddWorkViewController.h"
#import "YHYDesignerPersonViewController.h"
#import "YHYDesignerPTableViewCell.h"
#import "WSCustomPickerView.h"
#import "DCPaymentView.h"
#import "YHYAddCVButtonTableViewCell.h"
@interface YHYAddWorkViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,WSCustomPickerViewDelegate,YHYAddCVButtonDelegate>{
    NSInteger a;
    NSMutableDictionary *paramars;
    NSIndexPath *myIndepath;
    YHYDesignerPTableViewCell *cell;
    ResignModel *model;

}
//@property (strong, nonatomic) IBOutlet UITextField *workName;
@property (strong, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic,strong)UITextField *myTextfield;

@property (nonatomic,strong) UITextView *textV;
@property (nonatomic,strong) UILabel *uilabel;
@property (nonatomic,strong) UILabel *numLabel;

@property (nonatomic, retain) UITextField *nameTextField;
//@property (nonatomic, retain) UILabel *numLabel;
@property (nonatomic, assign) BOOL isSelect;
//@property (nonatomic, retain) UILabel *uilabel;
@property (nonatomic, assign) NSInteger intA;
@property (nonatomic, assign) NSInteger numChiese;
@property (nonatomic,retain) UIButton *button;



@property (nonatomic, strong) WSCustomPickerView *customPicker;
/**
 *  公司名称
 */
@property (nonatomic, strong) NSString *companyStr;
/**
 *  职位名称
 */
@property (nonatomic, strong) NSString *jobName;

@property (nonatomic, strong) NSString *ageStr;

@property (nonatomic, strong) NSString *age;


@end

@implementation YHYAddWorkViewController
- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    self.title = @"工作经历";
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LV;
    [self getHTTPNetWorking];
    paramars = [NSMutableDictionary new];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    

    // Do any additional setup after loading the view from its nib.
    _myTableView  = [UITableView new];
    _array = [NSMutableArray arrayWithObjects: @"公司名称",@"职位名称",@"时间段", nil];
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
    
        make.height.mas_equalTo(self.view);
    }];
    _myTableView.scrollEnabled = NO;
    _myTableView.rowHeight = ROWHEIGHT * AUTO_HEIGHT;
    _myTableView.delegate = self;
    _myTableView.dataSource  = self;
    self.myTableView.backgroundColor = LV;
    _myTableView.tableFooterView=[[UIView alloc]init];
    
   
    [_myTableView registerClass:[YHYDesignerPTableViewCell class] forCellReuseIdentifier:@"YHYDesignerPTableViewCell"];
    [_myTableView registerClass:[YHYAddCVButtonTableViewCell class] forCellReuseIdentifier:@"YHYAddCVButtonTableViewCell"];
    
    [_myTableView reloadData];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    
    else{
    return _array.count;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 300 * AUTO_HEIGHT;
    }
    return ROWHEIGHT * AUTO_HEIGHT;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    myIndepath = indexPath;
    static NSString *str = @"YHYDesignerPTableViewCell";
    cell = [tableView dequeueReusableCellWithIdentifier:str];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        
    
    cell.xmLabel.text = _array[indexPath.row];
    
    if (indexPath.row == 0) {//公司名称
        a = 0;
        if (_companyStr != nil) {
           cell.WXlabel.text = _companyStr;
            cell.WXlabel.text = model.companyname;

//            [paramars setObject:_companyStr forKey:@"companyname"];
        }
        cell.WXlabel.text = model.companyname;

    }
    if (indexPath.row == 1) {//职位名称
        a = 1;
        if (_jobName != nil) {

            cell.WXlabel.text = _jobName;
            cell.WXlabel.text = model.officename;

//            [paramars setObject:_jobName forKey:@"officename"];
        }
        cell.WXlabel.text = model.officename;

    }
        if (indexPath.row == 2) {//时间段
            if (_ageStr != nil) {

                cell.WXlabel.text = _ageStr;
                cell.WXlabel.text = model.period;

            }
            cell.WXlabel.text = model.period;

        }
            }
    if (indexPath.section == 1) {
        YHYAddCVButtonTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"YHYAddCVButtonTableViewCell"];
        cell1.delegate = self;
        cell1.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell1;
    }
    
    
    
    return cell;
}
#pragma mark - 协议
- (void)presentDelegate:(NSInteger)tag{
    [self AFNetWorking];
    [self.delegate presentAddWorkName:_companyStr];
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"dsad");
    if (indexPath.section == 0) {
     
    if (indexPath.row == 0) {//公司名字
        DCPaymentView *payAlert = [DCPaymentView new];
        //NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.numOfContent=9;
    payAlert.title = [_array objectAtIndex:indexPath.row];
      
        [payAlert show];
       
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            //self.contentAry[indexPath.row] = inputPwd;
            self.companyStr = inputPwd;
            if (self.companyStr != nil) {
                [paramars setObject:self.companyStr forKey:@"companyname"];

            }

            [self.myTableView reloadData];
            
        };
    }
    if (indexPath.row == 1) {//职位名称
        DCPaymentView *payAlert = [DCPaymentView new];
        //NSString *str = [_array objectAtIndex:indexPath.row+ 2 + indexPath.section];
        payAlert.numOfContent=9;
        payAlert.title = [_array objectAtIndex:indexPath.row];
        
        [payAlert show];
        
        payAlert.completeHandle = ^(NSString *inputPwd) {
            
            //self.contentAry[indexPath.row] = inputPwd;
            self.jobName = inputPwd;
            if (self.jobName!= nil) {
                [paramars setObject:self.jobName forKey:@"officename"];
            }
            [self.myTableView reloadData];
            
        };

    }
        if (indexPath.row == 2) {
            self.age = nil;
            self.customPicker = [[WSCustomPickerView alloc]initWithFrame:[[UIScreen mainScreen]bounds] type:3];
            self.customPicker.delegate = self;
            
            self.customPicker.titleStr = @"时间段";
            [self.view addSubview:_customPicker];
            [self.customPicker showPicker];

        }
    }
    
    
    
}

- (void)sendAddressInfo:(NSString *)address{
    
    if (a == 0) {//公司名字
        _companyStr = address;
    }
    if (a == 1) {//职位名称
        _jobName = address;
    }
    
    
}

//- (IBAction)save:(UIButton *)sender {
//    NSString *str = _textV.text;
//   
//    [self.navigationController popViewControllerAnimated:YES];
//    
//   // [self.delegate presentworkName:str];
//    [self.delegate presentAddWorkName:str];
//    
//}


-(void)Back:(UIButton *) goback
{
   // if (_companyStr.length <= 0) {
        //提示框
        NSString *title = NSLocalizedString(@"是否保存", nil);
        NSString *message = NSLocalizedString(@"", nil);
        NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
        NSString *otherButtonTitle = NSLocalizedString(@"保存", nil);
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
   // }
    
}
//上传数据
-(void)AFNetWorking{
    
    [paramars setObject:[LOCALSEVE objectForKey:@"UID"] forKey:@"uid"];
    [paramars setObject:[LOCALSEVE objectForKey:@"MCODE"] forKey:@"mcode"];
    [paramars setObject:[NSString stringWithFormat:@"%@,%@",@"2011",@"2016"] forKey:@"period"];
    NSString *url_string = [NSString  stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Home/CreateWexp"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = HTTPSET;
    [manager POST:url_string parameters:paramars constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"................%@",responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
    }];
    
}
// 获取本页数据
-(void)getHTTPNetWorking{

    NSString *url_string = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Gafaer/Home/vWexp"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = HTTPSET;
    [manager GET:url_string parameters:@{@"uid":[LOCALSEVE objectForKey:@"UID"],@"mcode":[LOCALSEVE objectForKey:@"MCODE"]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        model = [ResignModel new];
        [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"]];
        NSLog(@"工作经历------%@",responseObject);
        if (myIndepath.section == 0) {
            if (myIndepath.row == 0 && a == 0 ) {
                cell.WXlabel.text = model.companyname;
            }
            if (myIndepath.row == 1 && a == 1) {
                cell.WXlabel.text = model.officename;
                
            }
            if (myIndepath.row == 2) {
                cell.WXlabel.text = model.period;
                
            }
        }
        
        [self.myTableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误：%@",error);
        
    }];


}

@end
